package org.snusmumr.android.media;

import org.snusmumr.android.media.data.DatabaseProvider;
import org.snusmumr.android.media.data.DatabaseProvider.DatabaseMode;
import org.snusmumr.android.media.data.adapter.MediaListAdapter;
import org.snusmumr.android.media.data.adapter.MediaListAdapter.OnCheckedListener;
import org.snusmumr.android.media.util.Utils;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ListView;

public class SearchTab extends Activity {
	private MediaListAdapter adapter;
	
	private OnCheckedListener itemCheckedListener = new OnCheckedListener() {
		@Override
		public void onChecked(boolean isChecked, int position) {
//			if(checkedCount > 0) 
//				showButtonsPanel();
//			else
//				hideButtonsPanel();
		}

		private void hideButtonsPanel() {
			// TODO Auto-generated method stub
			
		}

		private void showButtonsPanel() {
			// TODO Auto-generated method stub
			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.search_tab);
        
//        this.searchField = (EditText) findViewById(R.id.ed_search_tab_search);
//        this.searchResult = (ListView) findViewById(R.id.v_search_media);
        
        final EditText filterEdit = (EditText) findViewById(R.id.ed_search_tab_search);
		filterEdit.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				Cursor c = adapter.runQueryOnBackgroundThread(s);
				adapter.getCursor().close();
				adapter.changeCursor(c);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override
			public void afterTextChanged(Editable s) {}
		});
        
		Cursor c = getSearchCursor("%*%");
        adapter = new MediaListAdapter(SearchTab.this, 
				c, 
				DatabaseProvider.getInstance(this).getDatabase(DatabaseMode.Read), 
				itemCheckedListener);
        adapter.setFilterQueryProvider(new FilterQueryProvider() {
			@Override
			public Cursor runQuery(CharSequence constraint) {
				if(constraint.length() > 1) {
					String searchPattern = new StringBuilder().append('%')
							.append(String.valueOf(constraint).toLowerCase())
							.append('%').toString();
					return getSearchCursor(searchPattern);
				} else
					return getSearchCursor("%*%");
			}
		});
        ((ListView) findViewById(R.id.v_search_media)).setAdapter(adapter);
	}

	private Cursor getSearchCursor(String searchPattern) {
		Cursor c = DatabaseProvider.getInstance(this).getDatabase(DatabaseMode.Read)
        		.rawQuery(getResources().getString(R.string.sql_search_items), new String[]{searchPattern, searchPattern});
		return c;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		Utils.closeCursor(adapter);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(adapter != null)
			adapter.getCursor().requery();
	}
}
